package setting

import (
	"../common"
	"../web"
	"strings"
)

type SetStationObj struct {
	common.BaseHandler
}

func (self *SetStationObj) Prepare() {
	stations := *common.FetchAllRowsPtr(
		`SELECT STATION,ISNULL(DESCRIP,'') 
           FROM SFC_STATION(NOLOCK) 
          ORDER BY STATION`,
		self.Usr.Conn, new(common.Station))

	self.TmplData["stations"] = stations
}

func (self *SetStationObj) ParameterCheck() bool {
	c_stat := strings.TrimSpace(self.Ctx.Params["sta"])
	c_desc := strings.TrimSpace(self.Ctx.Params["descrip"])
	if c_stat == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the Station.")
		return false
	}

	self.Sqlparas = append(self.Sqlparas, c_stat)
	self.Sqlparas = append(self.Sqlparas, c_desc)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Flashparas = []interface{}{strings.ToUpper(c_stat)}

	return true
}

func (self *SetStationObj) SqlExecute() bool {
	spRtnData := *common.FetchOneRowPtr(
		`sp_SetStation ?, ?, ?`,
		self.Usr.Conn, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
		return false
	}
	return true
}

func StationSetHandler(ctx *web.Context) {
	self := new(SetStationObj)
	self.Ctx = ctx
	self.Url = "/setstation"
	self.Html = "setstation.html"
	self.Rightlist = []string{"setstation"}
	self.Flashstr = "Set Station: [ %v ] finished."
	self.Do(self)
}
